//鉴权,有没有权利访问
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style


NProgress.configure({showSpinner:false})

const whiteList = ['/login','/404']  //白名单，放行

//前置守卫（导航守卫）
router.beforeEach( async (to,from,next)=>{
  NProgress.start()//进度条
  //存在token
  if(store.getters.token){
    //跳转主页,从登录页来
    if(to.path === '/login'){
      next('/')
      NProgress.done()
    }else{
      if( !store.getters.userId){
        //id不存在
        await store.dispatch('user/getUserInfo')
      }
      //放行
      next()
    }
  }else{
    //没token
    if(whiteList.includes(to.path)){ //查数组中的小项
      next()
    }else{
      next('/login') //不是白名单也不是从登录、404来
      NProgress.done
    }
  }
} ) 

//后置守卫(盘查完毕)
router.afterEach( ()=>{
  NProgress.done()
} )